package lambda;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;

/**
 * @author zhangying
 * @date 2020-02-12
 * @description
 */
public class Apple {
    String color;

    Integer weight;

    public Apple(String color) {
        this.color = color;
    }

    public Apple(String color, int weight) {
        this.color = color;
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "Apple{" +
                "color='" + color + '\'' +
                ", weight=" + weight +
                '}';
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Integer getWeight() {
        return weight;
    }

    public void setWeight(Integer weight) {
        this.weight = weight;
    }

    public static List<Apple> filterGreenApples(List<Apple> inventory) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple : inventory) {
            if ("green".equals(apple.getColor())) {
                result.add(apple);
            }
        }
        return result;
    }

    public static List<Apple> filterHeavyApples(List<Apple> inventory) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple : inventory) {
            if (apple.getWeight() > 150) {
                result.add(apple);
            }
        }
        return result;
    }


    public static boolean isGreenApple(Apple apple) {
        return "green".equals(apple.getColor());
    }

    public static boolean isHeavyApple(Apple apple) {
        return apple.getWeight() > 150;
    }

    /**
     * 传递方法
     */
    public static List<Apple> filterApples(List<Apple> inventory, Predicate<Apple> p) {
        List<Apple> result = new ArrayList<>();
        for (Apple apple : inventory) {
            if (p.test(apple)) {
                result.add(apple);
            }
        }
        return result;
    }

    public static void printPrettyApple(List<Apple> inventory, ApplePrinter printer) {
        for (Apple apple : inventory) {
            String output = printer.accept(apple);
            System.out.println("print is : " + output);
        }
    }

    public static void main(String[] args) {
        List<Apple> inventory = new ArrayList<>();
        inventory.add(new Apple("red", 300));
        inventory.add(new Apple("green", 200));
        List<Apple> gre = filterGreenApples(inventory);
        System.out.println("普通形式：" + gre.toString());

        //传递方法
        List<Apple> greenApple = filterApples(inventory, Apple::isGreenApple);
        System.out.println("传递方法 green：" + greenApple);

        List<Apple> heavyApple = filterApples(inventory, Apple::isHeavyApple);
        System.out.println("传递方法 heavy：" + heavyApple);

        List<Apple> lambdaGreenApple = filterApples(inventory, (Apple a) -> "green".equals(a.getColor()));
        System.out.println("传递lambda green：" + lambdaGreenApple);
        //行为参数化
        printPrettyApple(inventory, apple -> apple.color);

        //lambda 和方法引用
        //1、原始——创建Comparator的实现类比较
        inventory.sort(new AppleComparator());
        System.out.println("实现类："+inventory);
        //2、匿名内部类
        inventory.sort(new Comparator<Apple>() {
            @Override
            public int compare(Apple o1, Apple o2) {
                return o1.getWeight() - o2.getWeight();
            }
        });
        System.out.println("匿名内部类："+inventory);
        //3、lambda表达式
        inventory.sort((Apple a1,Apple a2) -> a1.getWeight().compareTo(a2.getWeight()));
        inventory.sort((a1,a2)->a1.getWeight().compareTo(a2.getWeight()));
        inventory.sort(Comparator.comparing(apple -> apple.getWeight()));
        //4、方法引用
        inventory.sort(Comparator.comparing(Apple::getWeight));
    }
}
